#! /usr/bin/env python
import os, shutil, zipfile
import waflib.Node, waflib.Options
import run

def options(opt):
    pass

def package_bob(self):
    bob = self.outputs[0].abspath()
    bob_light = os.path.join(self.env['DYNAMO_HOME'], 'share/java/bob-light.jar')
    shutil.copyfile(bob_light, bob)
    zip = zipfile.ZipFile(bob, 'a')
    for input in self.inputs:
        if not input.isdir():
            name = os.path.relpath(input.abspath(), 'content').replace('\\', '/')
            zip.write(input.srcpath(), name)
    zip.close()

    # If the platform has registered a shader compiler plugin (in the form of a jar),
    # we need to add that to the bob manifest so it can be picked up when executing
    shader_compiler_plugin_path = self.env['PLATFORM_SHADER_COMPILER_PLUGIN_JAR']
    if len(shader_compiler_plugin_path) != 0:
        shader_compiler_plugin_path = shader_compiler_plugin_path[0]
        shader_compiler_base        = os.path.basename(shader_compiler_plugin_path)
        shader_compiler_new_path    = os.path.join(os.path.dirname(bob), shader_compiler_base)
        shutil.copyfile(shader_compiler_plugin_path, shader_compiler_new_path)

        extra_manifest = "Class-Path: %s\n" % shader_compiler_base
        extra_manifest_path = self.outputs[0].change_ext('.txt').abspath()

        with open(extra_manifest_path, 'w') as out_f:
            out_f.write(extra_manifest)

        run.env_command(dict(os.environ), ['jar', 'umf', extra_manifest_path, bob])

def remap_material_output(msg):
    if msg.vertex_program == "/materials/debug.vp" and msg.fragment_program == "/materials/debug.fp":
        return "debug.spc"
    return None

def build(bld):
    obj = bld(source = bld.path.ant_glob('materials/*', excl = ['materials/*.fp', 'materials/*.vp']),
              remap_material_output = remap_material_output)

    bld(features='barchive',
       source_root='default/content',
       content_root='../content',
       resource_name='builtins',
       source = bld.path.ant_glob([
           'builtins/input/**/*',
           'builtins/render/**/*',
           'builtins/fonts/**/*',
           'builtins/connect/**/*',
           'builtins/materials/**/*',
           'builtins/graphics/**/*',
           'builtins/scripts/**/*'],
           excl = ['**/*.ttf', '**/*.texture_profiles', '**/LICENSE', '**/*.vp', '**/*.fp', '**/*.cp']),
       use_compression=True)

    for d in ['fonts', 'input', 'render', 'materials', 'graphics', 'scripts', 'docs', 'ca-certificates']:
        bld.install_files('${PREFIX}/content/builtins/%s' % d, bld.path.ant_glob('builtins/%s/*' % d))

    start_dir = bld.path.find_dir('builtins')
    bld.install_files('${PREFIX}/content/builtins', start_dir.ant_glob('manifests/**/*'), cwd = start_dir, relative_trick = True)
    bld.install_files('${PREFIX}/content/builtins', start_dir.ant_glob('assets/**/*'), cwd = start_dir, relative_trick = True)
    bld.install_files('${PREFIX}/content/builtins', start_dir.ant_glob('fonts/**/*'), cwd = start_dir, relative_trick = True)

    if not waflib.Options.options.skip_build_tests:
        bld(name = 'bob-engine',
            source = bld.path.ant_glob('builtins/**'),
            target = 'bob-engine.jar',
            rule = package_bob,
            always = True)
        bld.install_files('${PREFIX}/share/java', 'bob-engine.jar')


def configure(conf):
    pass
